package com.xiaobai.java_core.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author 王天文
 * @Date 2024/12/12 11:00
 * @Description:
 */
public class FrameDialogDemo {

    public static void main(String[] args) {

        // 获取屏幕的宽度和高度
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;

        JFrame frame = new JFrame("主窗口");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        List<JDialog> dialogList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    Random random = new Random();
                    int x = random.nextInt(screenWidth - 300);
                    int y = random.nextInt(screenHeight - 200);
                    createDialog(frame, x, y, dialogList);
                }
            });
            thread.start();
            if (i <= 3) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        // 释放frame资源
//        frame.dispose();
    }

    private static void createDialog(JFrame frame, int x, int y, List<JDialog> dialogList) {
        // 创建JDialog
        JDialog dialog = new JDialog(frame, "温馨提示", true);
        dialogList.add(dialog);

        // 创建面板
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        // 配置通用约束条件, 用于水平和垂直居中
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.CENTER;

        // 创建提示标签并添加到对话框面板
        JLabel label = new JLabel("你该下班了！");
        // 标签设置在第一行
        gbc.gridy = 0;
        panel.add(label, gbc);

        // 创建按钮面板用于放置确认取消按钮
        JPanel buttonPanel = new JPanel();

        // 创建确认按钮
        JButton confirmButton = new JButton("立马下班");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (JDialog hisDialog : dialogList) {
                    hisDialog.dispose();
                }
            }
        });
        //添加确认按钮
        buttonPanel.add(confirmButton);

        // 创建取消按钮
        JButton cancelButton = new JButton("加会儿班");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                createDialog(frame, x + 50, y + 50, dialogList);
            }
        });
        buttonPanel.add(cancelButton);

        // 按钮设置在第二行
        gbc.gridy = 2;
        panel.add(buttonPanel, gbc);

        dialog.setContentPane(panel);
        dialog.pack();

        // 设置对话框位置
        dialog.setLocation(x, y);
        dialog.setSize(250, 150);

        dialog.setVisible(true);
    }
}
